﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using KinectPG.LibraryTestGame.Enums;

namespace KinectPG.LibraryTestGame
{
    class Fruit : GameObject
    {
        #region fields

        private float speed = 1.0f;
        private float startDelay = 0;
        private int collision = 0;
        private bool hadCollision = false;
        #endregion

        GraphicsDevice graphics;
        Random randomGenerator;
        ContentManager content;
        

        public Fruit(SpriteBatch sb, GraphicsDevice GraphicsDevice, ContentManager content, float? scale)
            : base(sb)
        {
            randomGenerator = new Random(DateTime.Now.Millisecond);
            graphics = GraphicsDevice;
            if(scale.HasValue)
                this.Scale = scale.Value;
            this.content = content;
            this.objectTexture = content.Load<Texture2D>(FruitsNames.GetFruit(randomGenerator.Next() % 6));
            this.Position = new Vector2(randomGenerator.Next(0, (int)(1280 - objectTexture.Width * this.Scale)), -objectTexture.Height * this.Scale);
            this.speed = (float)((randomGenerator.NextDouble() + 0.5f) * 2);
            this.IsAlive = true;
        }

        public Fruit(SpriteBatch sb, GraphicsDevice GraphicsDevice)
            : base(sb)
        {
            Random rand = new Random();
            this.Position = new Vector2(rand.Next(0, 1280), rand.Next(0, 1024));
            graphics = GraphicsDevice;
        }

        public override void Update(GameTime gameTime)
        {
            if (!this.IsAlive)
            {
                this.IsAlive = true;
                this.Position = new Vector2(randomGenerator.Next(0, 1280), -objectTexture.Height * this.Scale);
                this.objectTexture = content.Load<Texture2D>(FruitsNames.GetFruit(randomGenerator.Next() % 6));

                this.startDelay = randomGenerator.Next(0, 1000);
                this.speed = (float)((randomGenerator.NextDouble() + 0.5f) * 2);
            }

            if (collision > 0)
            {
                collision--;
                return;
            }

            if (hadCollision)
            {
                hadCollision = false;
                this.IsAlive = false;
                return;
            }

            if (this.startDelay > 0)
            {
                startDelay--;
                return;
            }

            this.Position.Y += 1 * speed;
            if (this.Position.Y > this.graphics.DisplayMode.Height) this.IsAlive = false;
        }

        public override void Draw(GameTime gameTime)
        {
            //this.Position.Y += 1;
            //this.SpriteBatch.Draw(this.objectTexture, new Rectangle((int)this.Position.X, (int)this.Position.Y, this.objectTexture.Width, this.objectTexture.Height), Color.White,);
            if(collision >= 0 && collision % 10 == 0)
                this.SpriteBatch.Draw(this.objectTexture, new Vector2(this.Position.X, this.Position.Y), null, Color.White, 0.0f, new Vector2(0,0), this.Scale, SpriteEffects.None, 0);
            else this.SpriteBatch.Draw(new Texture2D(graphics, 1, 1), new Vector2(this.Position.X, this.Position.Y), null, Color.White, 0.0f, new Vector2(0, 0), this.Scale, SpriteEffects.None, 0);
        }


        public int CollisionAction()
        {
            hadCollision = true;
            if (collision == 0)
            {
                collision += 50;
                return 1;
            }
            return 0;
        }

    }
}
